package com.ruoyi.keno.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.keno.entity.KenoGuess;
import com.ruoyi.keno.entity.KenoHistory;
import com.ruoyi.keno.enums.BigEnums;
import com.ruoyi.keno.enums.LuEnums;
import com.ruoyi.keno.mapper.KenoHistoryMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.keno.mapper.KenoGuessMapper;
import com.ruoyi.keno.service.IKenoGuessService;
import com.ruoyi.common.core.text.Convert;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ziyeibin
 * @date 2020-11-06
 */
@Service
public class KenoGuessServiceImpl implements IKenoGuessService 
{
    @Autowired
    private KenoGuessMapper kenoGuessMapper;
    @Autowired
    private KenoHistoryMapper kenoHistoryMapper;


    @Override
    public void guessNextResult(List<KenoHistory> list) {
        if(list.size()>50) {
            Collections.reverse(list);
            this.三连续渐(list);
            this.五行土火(list);
            this.五行金木(list);
            this.大小渐节奏(list);
            this.态势大小(list);
            this.连续大小(list);
        }
    }

    @Override
    public void guessResult() {
        List<KenoGuess> guessList = kenoGuessMapper.findGuessResultIsNull();
        for(KenoGuess guess: guessList){
            KenoHistory keno = kenoHistoryMapper.selectByPrimaryKey(guess.getIssueNo());
            if(keno!=null) {
                if(keno.getBig()==1){
                    guess.setGuessResult(-1);
                }else {
                    if (guess.getGuess().equals(BigEnums.getBigNameByCode(keno.getBig()))) {
                        guess.setGuessResult(0);
                    } else {
                        guess.setGuessResult(1);
                    }
                }
                kenoGuessMapper.updateByPrimaryKeySelective(guess);
            }
        }
    }


    private void 三连续渐(List<KenoHistory> list){
        List<KenoHistory> lists = new ArrayList<>();
        lists.addAll(list.subList(0,9));
        Collections.reverse(lists);
        boolean flag = true;
        for(int i=1;i<8;i++){
            KenoHistory keno = lists.get(i);
            if((keno.getTotal()>lists.get(i-1).getTotal() && keno.getTotal()>lists.get(i+1).getTotal()) ){
                if(i>2 && i<lists.size()-1){
                   if(!(keno.getTotal()>lists.get(i-1).getTotal() && lists.get(i-1).getTotal()>lists.get(i-2).getTotal()  && lists.get(i-2).getTotal()>lists.get(i-3).getTotal())){
                       flag = false;
                   }
                }
            }
            if((keno.getTotal()<lists.get(i-1).getTotal() && keno.getTotal()<lists.get(i+1).getTotal()) ){
                if(i>2 && i<lists.size()-1){
                    if(!(keno.getTotal()<lists.get(i-1).getTotal() && lists.get(i-1).getTotal()<lists.get(i-2).getTotal() && lists.get(i-2).getTotal()<lists.get(i-3).getTotal())){
                        flag = false;
                    }
                }
            }
        }
        if(flag && ((lists.get(0).getTotal()>lists.get(1).getTotal() && lists.get(0).getTotal()>810) || lists.get(0).getTotal()<lists.get(1).getTotal() && lists.get(0).getTotal()<810)){
            KenoGuess guess = new KenoGuess();
            guess.setCreateTime(new Date());
            guess.setIssueNo(list.get(0).getIssueNo() + 1L);
            guess.setArea(list.get(0).getArea());
            if(lists.get(0).getTotal()>lists.get(1).getTotal() && lists.get(0).getTotal()>810) {
                guess.setGuess("大");
            }
            if(lists.get(0).getTotal()<lists.get(1).getTotal() && lists.get(0).getTotal()<810) {
                guess.setGuess("小");
            }
            guess.setGuessType(LuEnums.三连续渐.getLuCode());

            StringBuffer issueNoArr = new StringBuffer();
            StringBuffer bigArr = new StringBuffer();
            StringBuffer numArr = new StringBuffer();
            for (int i = 0; i < 9; i++) {
                KenoHistory keno = lists.get(i);
                issueNoArr.append("," + keno.getIssueNo());
                bigArr.append(","+BigEnums.getBigNameByCode(keno.getBig()));
                numArr.append(","+keno.getTotal());
            }
            guess.setGuessBigArr(bigArr.substring(1).toString());
            guess.setGuessIssueNoArr(issueNoArr.substring(1).toString());
            guess.setGuessNumArr(numArr.substring(1).toString());
            guess.setGuessData(JSONObject.toJSON(lists).toString());
            kenoGuessMapper.insertSelective(guess);
        }


    }


    private void 连续大小(List<KenoHistory> list){
        List<KenoHistory> lists = new ArrayList<>();
        lists.addAll(list.subList(0,17));
        Collections.reverse(lists);
        boolean flag = true;
        StringBuffer issueNoArr = new StringBuffer();
        StringBuffer bigArr = new StringBuffer();
        StringBuffer numArr = new StringBuffer();
        for(int i=1;i<lists.size();i++){
            KenoHistory keno = lists.get(i);
            issueNoArr.append(","+keno.getIssueNo());
            bigArr.append(","+BigEnums.getBigNameByCode(keno.getBig()));
            numArr.append(","+keno.getTotal());
            if(!keno.getBig().equals(lists.get(i-1).getBig())){
                if(i<lists.size()-1 && !keno.getBig().equals(lists.get(i+1).getBig())){
                    flag = false;
                }
            }
        }
        if(flag){
            KenoGuess guess = new KenoGuess();
            guess.setCreateTime(new Date());
            guess.setIssueNo(list.get(0).getIssueNo()+1L);
            guess.setArea(list.get(0).getArea());
            guess.setGuess(BigEnums.getBigNameByCode(lists.get(lists.size()-1).getBig()));
            guess.setGuessType(LuEnums.连续大小.getLuCode());
            guess.setGuessIssueNoArr(issueNoArr.substring(1).toString());
            guess.setGuessBigArr(bigArr.substring(1).toString());
            guess.setGuessNumArr(numArr.substring(1).toString());
            guess.setGuessData(JSONObject.toJSON(lists).toString());
            kenoGuessMapper.insertSelective(guess);
        }
    }

    //大小渐变
    private void 大小渐节奏(List<KenoHistory> list){
        List<KenoHistory> lists = new ArrayList<>();
        lists.addAll(list.subList(0,6));
        Collections.reverse(lists);
        if(lists.get(0).getTotal()>lists.get(1).getTotal() && lists.get(1).getTotal()<lists.get(2).getTotal() && lists.get(2).getTotal()>lists.get(3).getTotal()
                && lists.get(3).getTotal()<lists.get(4).getTotal()  && lists.get(4).getTotal()>lists.get(5).getTotal()){

            KenoGuess guess = new KenoGuess();
            guess.setCreateTime(new Date());
            guess.setIssueNo(list.get(0).getIssueNo() + 1L);
            guess.setArea(list.get(0).getArea());
            guess.setGuess("大");
            if(lists.get(4).getTotal()>lists.get(2).getTotal() && lists.get(2).getTotal()>lists.get(0).getTotal()) {
                guess.setGuessType(LuEnums.大小渐节奏.getLuCode());
            }else{
                guess.setGuessType(LuEnums.大小节奏.getLuCode());
            }
            if(lists.get(4).getBig()==BigEnums.大.getCode() &&
                ((lists.get(4).getWuxing().equals("土") && lists.get(2).getWuxing().equals("土")) || !lists.get(4).getWuxing().equals("土"))) {
                StringBuffer issueNoArr = new StringBuffer();
                StringBuffer bigArr = new StringBuffer();
                StringBuffer numArr = new StringBuffer();
                for (int i = 0; i < 6; i++) {
                    KenoHistory keno = lists.get(i);
                    issueNoArr.append("," + keno.getIssueNo());
                    bigArr.append("," + BigEnums.getBigNameByCode(keno.getBig()));
                    numArr.append("," + keno.getTotal());
                }
                guess.setGuessBigArr(bigArr.substring(1).toString());
                guess.setGuessIssueNoArr(issueNoArr.substring(1).toString());
                guess.setGuessNumArr(numArr.substring(1).toString());
                guess.setGuessData(JSONObject.toJSON(lists).toString());
                kenoGuessMapper.insertSelective(guess);
            }
        }
        if(lists.get(0).getTotal()<lists.get(1).getTotal() && lists.get(1).getTotal()>lists.get(2).getTotal() && lists.get(2).getTotal()<lists.get(3).getTotal()
                && lists.get(3).getTotal()>lists.get(4).getTotal()  && lists.get(4).getTotal()<lists.get(5).getTotal()){
            KenoGuess guess = new KenoGuess();
            guess.setCreateTime(new Date());
            guess.setIssueNo(list.get(0).getIssueNo()+1L);
            guess.setArea(list.get(0).getArea());
            guess.setGuess("小");
            if(lists.get(0).getTotal()>lists.get(2).getTotal() && lists.get(2).getTotal()>lists.get(4).getTotal() &&
            ((lists.get(4).getWuxing().equals("金") && lists.get(2).getWuxing().equals("金")) || !lists.get(4).getWuxing().equals("金"))) {
                guess.setGuessType(LuEnums.大小渐节奏.getLuCode());
            }else{
                guess.setGuessType(LuEnums.大小节奏.getLuCode());
            }
            if(lists.get(4).getBig()==BigEnums.小.getCode() && guess.getGuessType()!=null) {
                StringBuffer issueNoArr = new StringBuffer();
                StringBuffer bigArr = new StringBuffer();
                StringBuffer numArr = new StringBuffer();
                for (int i = 0; i < 6; i++) {
                    KenoHistory keno = lists.get(i);
                    issueNoArr.append("," + keno.getIssueNo());
                    bigArr.append("," + BigEnums.getBigNameByCode(keno.getBig()));
                    numArr.append("," + keno.getTotal());
                }
                guess.setGuessBigArr(bigArr.substring(1).toString());
                guess.setGuessIssueNoArr(issueNoArr.substring(1).toString());
                guess.setGuessNumArr(numArr.substring(1).toString());
                guess.setGuessData(JSONObject.toJSON(lists).toString());
                kenoGuessMapper.insertSelective(guess);
            }
        }
    }

    private void 五行土火(List<KenoHistory> list){
        List<KenoHistory> lists = new ArrayList<>();
        lists.addAll(list.subList(0,9));
        Collections.reverse(lists);

        int i = 0;
        StringBuffer issueNoArr = new StringBuffer();
        StringBuffer bigArr = new StringBuffer();
        StringBuffer numArr = new StringBuffer();
        for(KenoHistory keno:lists){
            issueNoArr.append(","+keno.getIssueNo());
            bigArr.append(","+BigEnums.getBigNameByCode(keno.getBig()));
            numArr.append(","+keno.getTotal());
            if(keno.getWuxing().equals("土") || keno.getWuxing().equals("火")){
                i++;
            }
        }
        if(i>6){
            KenoGuess guess = new KenoGuess();
            guess.setCreateTime(new Date());
            guess.setIssueNo(list.get(0).getIssueNo()+1L);
            guess.setArea(list.get(0).getArea());
            guess.setGuess("大");
            guess.setGuessType(LuEnums.五行土火.getLuCode());
            guess.setGuessIssueNoArr(issueNoArr.substring(1).toString());
            guess.setGuessBigArr(bigArr.substring(1).toString());
            guess.setGuessNumArr(numArr.substring(1).toString());
            guess.setGuessData(JSONObject.toJSON(lists).toString());
            kenoGuessMapper.insertSelective(guess);
        }
    }

    private void 五行金木(List<KenoHistory> list){
        List<KenoHistory> lists = new ArrayList<>();
        lists.addAll(list.subList(0,9));
        Collections.reverse(lists);
        int i = 0;
        StringBuffer issueNoArr = new StringBuffer();
        StringBuffer bigArr = new StringBuffer();
        StringBuffer numArr = new StringBuffer();
        for(KenoHistory keno:lists){
            issueNoArr.append(","+keno.getIssueNo());
            bigArr.append(","+BigEnums.getBigNameByCode(keno.getBig()));
            numArr.append(","+keno.getTotal());
            if(keno.getWuxing().equals("金") || keno.getWuxing().equals("木")){
                i++;
            }
        }
        if(i>6){
            KenoGuess guess = new KenoGuess();
            guess.setCreateTime(new Date());
            guess.setIssueNo(list.get(0).getIssueNo()+1L);
            guess.setArea(list.get(0).getArea());
            guess.setGuess("小");
            guess.setGuessType(LuEnums.五行金木.getLuCode());
            guess.setGuessIssueNoArr(issueNoArr.substring(1).toString());
            guess.setGuessBigArr(bigArr.substring(1).toString());
            guess.setGuessNumArr(numArr.substring(1).toString());
            guess.setGuessData(JSONObject.toJSON(lists).toString());
            kenoGuessMapper.insertSelective(guess);
        }
    }

    private void 态势大小(List<KenoHistory> list){
        List<KenoHistory> lists = new ArrayList<>();
        lists.addAll(list.subList(0,29));
        Collections.reverse(lists);
        int big = 0;
        int small = 0;
        StringBuffer issueNoArr = new StringBuffer();
        StringBuffer bigArr = new StringBuffer();
        StringBuffer numArr = new StringBuffer();
        for(KenoHistory keno:lists){
            issueNoArr.append(","+keno.getIssueNo());
            bigArr.append(","+BigEnums.getBigNameByCode(keno.getBig()));
            numArr.append(","+keno.getTotal());
            if(keno.getBig()==0){
                big++;
            }else if(keno.getBig()==2){
                small++;
            }
        }
        if(big>20  || small>20){
            KenoGuess guess = new KenoGuess();
            guess.setCreateTime(new Date());
            guess.setIssueNo(list.get(0).getIssueNo()+1L);
            guess.setArea(list.get(0).getArea());
            if(small>20 && list.get(0).getBig().equals(BigEnums.小.getCode())){
                guess.setGuess("小");
            }else if(big>20  && list.get(0).getBig().equals(BigEnums.大.getCode())){
                guess.setGuess("大");
            }
            if(StringUtils.isNoneBlank(guess.getGuess())) {
                guess.setGuessType(LuEnums.态势大小.getLuCode());
                guess.setGuessIssueNoArr(issueNoArr.substring(1).toString());
                guess.setGuessBigArr(bigArr.substring(1).toString());
                guess.setGuessNumArr(numArr.substring(1).toString());
                guess.setGuessData(JSONObject.toJSON(lists).toString());
                kenoGuessMapper.insertSelective(guess);
            }
        }
    }


}
